<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单id" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入订单id" clearable @keyup.enter.native="handleQueryBy" />

      </el-form-item>
      <el-form-item label="客户电话" prop="contactsPhone">
        <el-input v-model="queryParams.contactsPhone" placeholder="请输入服务项名称" clearable
          @keyup.enter.native="handleQueryBy" />
      </el-form-item>

      <el-form-item label="客户电话" prop="contactsPhone">
        <el-input v-model="queryParams.contactsPhone" placeholder="请输入客户电话" clearable
          @keyup.enter.native="handleQueryBy" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-col :span="6">
      <el-form-item label="下单时间">
        <el-date-picker v-model="queryParams.startDate" type="date" placeholder="开始日期" value-format="yyyy-MM-dd" />
      </el-form-item>
    </el-col>

    <el-col :span="6">
      <el-form-item>
        <el-date-picker v-model="queryParams.endDate" type="date" placeholder="结束日期" value-format="yyyy-MM-dd" />
      </el-form-item>
    </el-col>
    <el-row>
      <el-col :span="6">
        <el-form-item label="订单状态">
          <el-select v-model="queryParams.ordersStatus" placeholder="请选择" clearable>
            <el-option label="请选择" value="" />
            <el-option label="待支付" value="0" />
            <el-option label="派单中" value="100" />
            <el-option label="待服务" value="200" />
            <el-option label="服务中" value="300" />
            <el-option label="待评价" value="400" />
            <el-option label="已完成" value="500" />
            <el-option label="已取消" value="600" />
            <el-option label="已关闭" value="700" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="支付状态">
          <el-select v-model="queryParams.payStatus" placeholder="请选择" clearable>
            <el-option label="请选择" value="" />
            <el-option label="未支付" value="0" />
            <el-option label="已支付" value="1" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="退款状态">
          <el-select v-model="queryParams.refundStatus" placeholder="请选择" clearable>
            <el-option label="请选择" value="" />
            <el-option label="退款成功" value="1" />
            <el-option label="退款中" value="2" />
            <el-option label="退款失败" value="3" />
          </el-select>
        </el-form-item>
      </el-col>
      </br>
      </br>
      <el-col :span="6">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryBy">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-col>
    </el-row>



    <el-table v-loading="loading" :data="searchList" @selection-change="handleSelectionChange" style="width: 100%"
      border>
      <!-- <el-table-column
        type="selection"
        width="55"
        align="center"
      /> -->
      <el-table-column label="订单" align="center" prop="id" width="220" />
      <el-table-column label="服务项名称" align="center" prop="serveItemName" width="140" />
      <el-table-column label="订单状态" align="center" prop="ordersStatus" width="100">
        <template slot-scope="scope">
          <span :class="formatOrderStatus(scope.row.ordersStatus)">
            {{ getOrderStatusText(scope.row.ordersStatus) }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="支付状态" align="center" prop="payStatus" width="140">
        <template slot-scope="scope">
          <span :class="formatPayStatus(scope.row.payStatus)">
            {{ getPayStatus(scope.row.payStatus) }}
          </span>
        </template>
      </el-table-column>

      <el-table-column label="退款状态" align="center" prop="refundStatus" width="140">
        <template slot-scope="scope">
          {{ formatRefundStatus(scope.row.refundStatus) }}
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="totalAmount" width="140" />
      <el-table-column label="实付金额" align="center" prop="realPayAmount" width="140" />
      <!-- 其他列使用默认宽度，并且可以通过滚动查看 -->
      <el-table-column label="服务地址" align="center" prop="serveAddress" width="280" />
      <el-table-column label="客户电话" align="center" prop="contactsPhone" width="180" />
      <el-table-column label="客户姓名" align="center" prop="contactsName" />
      <el-table-column label="下单时间" align="center" prop="evaluationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.evaluationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评价状态" align="center" prop="evaluationStatus" width="100">
        <template slot-scope="scope">
          {{ formatEvaluationStatus(scope.row.evaluationStatus) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="routerPush(scope.row.id)"
            v-hasPermi="['serve:search:view']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单所属人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入订单所属人" />
        </el-form-item>
        <el-form-item label="服务类型id" prop="serveTypeId">
          <el-input v-model="form.serveTypeId" placeholder="请输入服务类型id" />
        </el-form-item>
        <el-form-item label="服务类型名称" prop="serveTypeName">
          <el-input v-model="form.serveTypeName" placeholder="请输入服务类型名称" />
        </el-form-item>
        <el-form-item label="服务项id" prop="serveItemId">
          <el-input v-model="form.serveItemId" placeholder="请输入服务项id" />
        </el-form-item>
        <el-form-item label="服务项名称" prop="serveItemName">
          <el-input v-model="form.serveItemName" placeholder="请输入服务项名称" />
        </el-form-item>
        <el-form-item label="服务项图片" prop="serveItemImg">
          <el-input v-model="form.serveItemImg" placeholder="请输入服务项图片" />
        </el-form-item>
        <el-form-item label="服务单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入服务单位" />
        </el-form-item>
        <el-form-item label="服务id" prop="serveId">
          <el-input v-model="form.serveId" placeholder="请输入服务id" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="购买数量" prop="purNum">
          <el-input v-model="form.purNum" placeholder="请输入购买数量" />
        </el-form-item>
        <el-form-item label="订单金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="realPayAmount">
          <el-input v-model="form.realPayAmount" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入优惠金额" />
        </el-form-item>
        <el-form-item label="城市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入城市编码" />
        </el-form-item>
        <el-form-item label="服务地址" prop="serveAddress">
          <el-input v-model="form.serveAddress" placeholder="请输入服务地址" />
        </el-form-item>
        <el-form-item label="客户电话" prop="contactsPhone">
          <el-input v-model="form.contactsPhone" placeholder="请输入客户电话" />
        </el-form-item>
        <el-form-item label="客户姓名" prop="contactsName">
          <el-input v-model="form.contactsName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="服务开始时间" prop="serveStartTime">
          <el-date-picker clearable v-model="form.serveStartTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择服务开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="form.payTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下单时间" prop="evaluationTime">
          <el-date-picker clearable v-model="form.evaluationTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付服务交易单号" prop="tradingOrderNo">
          <el-input v-model="form.tradingOrderNo" placeholder="请输入支付服务交易单号" />
        </el-form-item>
        <el-form-item label="第三方支付的交易号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入第三方支付的交易号" />
        </el-form-item>
        <el-form-item label="支付服务退款单号" prop="refundNo">
          <el-input v-model="form.refundNo" placeholder="请输入支付服务退款单号" />
        </el-form-item>
        <el-form-item label="第三方支付的退款单号" prop="refundId">
          <el-input v-model="form.refundId" placeholder="请输入第三方支付的退款单号" />
        </el-form-item>
        <el-form-item label="支付渠道" prop="tradingChannel">
          <el-input v-model="form.tradingChannel" placeholder="请输入支付渠道" />
        </el-form-item>
        <el-form-item label="用户端是否展示" prop="display">
          <el-input v-model="form.display" placeholder="请输入用户端是否展示" />
        </el-form-item>
        <el-form-item label="排序字段，serve_start_time秒级时间戳+订单id后六位" prop="sortBy">
          <el-input v-model="form.sortBy" placeholder="请输入排序字段，serve_start_time秒级时间戳+订单id后六位" />
        </el-form-item>
        <el-form-item label="实际服务完成时间" prop="realServeEndTime">
          <el-date-picker clearable v-model="form.realServeEndTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择实际服务完成时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style>
/* 添加滚动条样式 */
.app-container {
  height: 100vh;
  /* 根据实际情况调整高度 */
  overflow: auto;
  /* 添加滚动条 */
}

.el-table {
  width: auto !important;
  /* 确保表格宽度自动 */
}

.status-success::before {
  content: '•';
  color: #67c23a;
  /* 绿色文字 */
  display: inline-block;
  position: absolute;
  left: -20px;
  width: 20px;
  height: 20px;
  font-size: 23px;
  border-radius: 50%;
}

.status-cancel::before {
  content: '•';
  color: #f56c6c;
  /* 红色文字 */
  display: inline-block;
  position: absolute;
  left: -20px;
  width: 20px;
  height: 20px;
  font-size: 23px;
  border-radius: 50%;
}

.status-pending::before {
  content: '•';
  color: #e6a23c;
  /* 黄色文字 */
  display: inline-block;
  position: absolute;
  left: -20px;
  width: 20px;
  height: 20px;
  font-size: 23px;
  border-radius: 50%;
}

.status-success,
.status-pending,
.status-cancel {
  display: inline-block;
  position: relative;
  left: 6px;
  border-radius: 50%;
  text-align: center;
}
</style>



<script>
import { addSearch, getSearch, listSearch, updateSearch } from "@/api/serve/search";

export default {
  name: "Search",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单列表表格数据
      searchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 实际服务完成时间时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
        id: null, // 订单ID
        ordersStatus: null, // 订单状态
        payStatus: null, // 支付状态
        refundStatus: null, // 退款状态
        contactsPhone: null, // 客户电话
        startDate: null, // 订单开始日期
        endDate: null, // 订单结束日期
        createTime: null, // 创建时间
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        purNum: [
          { required: true, message: "购买数量不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "订单金额不能为空", trigger: "blur" }
        ],
        realPayAmount: [
          { required: true, message: "实付金额不能为空", trigger: "blur" }
        ],
        discountAmount: [
          { required: true, message: "优惠金额不能为空", trigger: "blur" }
        ],
        cityCode: [
          { required: true, message: "城市编码不能为空", trigger: "blur" }
        ],
        serveAddress: [
          { required: true, message: "服务地址不能为空", trigger: "blur" }
        ],
        contactsPhone: [
          { required: true, message: "客户电话不能为空", trigger: "blur" }
        ],
        contactsName: [
          { required: true, message: "客户姓名不能为空", trigger: "blur" }
        ],
        serveStartTime: [
          { required: true, message: "服务开始时间不能为空", trigger: "blur" }
        ],
        evaluationStatus: [
          { required: true, message: "评价状态不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listSearch(this.queryParams).then(response => {
        // 假设后端返回的数据结构是 { rows: [], total: 0 }
        this.searchList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        console.error("查询失败：", error);
        this.loading = false;
        // 这里可以添加错误处理，例如显示一个错误消息
      });
    },
    getListBy() {
      this.loading = true;
      getSearch(this.queryParams).then(response => {
        // 假设后端返回的数据结构是 { rows: [], total: 0 }
        this.searchList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(error => {
        console.error("查询失败：", error);
        this.loading = false;
        // 这里可以添加错误处理，例如显示一个错误消息
      });
    },
    getOrderStatusText(status) {
      const statusMap = {
        '0': '待支付',
        '100': '派单中',
        '200': '待服务',
        '300': '服务中',
        '400': '待评价',
        '500': '已完成',
        '600': '已取消',
        '700': '已关闭',
      };
      return statusMap[status] || '未知状态';
    },
    formatOrderStatus(status) {
      if (status == 500) {
        return 'status-success';
      } else if (status == 600) {
        return 'status-cancel';
      } else {
        return 'status-pending';
      }
    },
    // 支付状态映射
    getPayStatus(status) {
      const statusMap = {
        0: '未支付',
        1: '已支付',
        2: '支付中',
        4: '支付失败'
      };
      return statusMap[status] || '';
    },
    formatPayStatus(status) {
      if (status == 0) {
        return 'status-success';
      } else if (status == 4) {
        return 'status-cancel';
      } else {
        return 'status-pending';
      }
    },

    // 退款状态映射
    formatRefundStatus(status) {
      const statusMap = {
        0: '退款失败',
        1: '退款成功',
        2: '退款中'
      };
      return statusMap[status] || '';
    },

    // 评价状态映射
    formatEvaluationStatus(status) {
      const statusMap = {
        1: '已评价'
        // 如果有其他状态，可以在这里继续添加
      };
      return statusMap[status] || '未评价'; // 默认为已评价
    },

    /** 搜索按钮操作 */
    handleQuery() {
      // 重置页码
      this.queryParams.pageNum = 1;
      // 调用getList方法重新加载数据
      this.getListBy();
    },

    //页面跳转
    routerPush(id) {
      this.$router.push({ name: 'OrderDetails', params: { id: id } });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        serveTypeId: null,
        serveTypeName: null,
        serveItemId: null,
        serveItemName: null,
        serveItemImg: null,
        unit: null,
        serveId: null,
        ordersStatus: null,
        payStatus: null,
        refundStatus: null,
        price: null,
        purNum: null,
        totalAmount: null,
        realPayAmount: null,
        discountAmount: null,
        cityCode: null,
        serveAddress: null,
        contactsPhone: null,
        contactsName: null,
        serveStartTime: null,
        lon: null,
        lat: null,
        payTime: null,
        evaluationTime: null,
        evaluationStatus: null,
        tradingOrderNo: null,
        transactionId: null,
        refundNo: null,
        refundId: null,
        tradingChannel: null,
        display: null,
        sortBy: null,
        realServeEndTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListBy();
    },


    /** 重置按钮操作 */
    resetQuery() {
      // 重置搜索条件
      this.queryParams.id = null;
      this.queryParams.contactsPhone = null;
      this.queryParams.daterangeCreateTime = [];
      this.queryParams.ordersStatus = null;
      this.queryParams.payStatus = null;
      this.queryParams.refundStatus = null;
      this.getList();
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSearch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSearch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
